﻿using System;
using System.Collections.Generic;
using Color = System.Drawing.Color;
using SwinGame;

namespace Gundam_2._0_Alpha.src
{
    public class MissionLoader:  IAction
    {
        private string[] missionFile;
        private Dictionary<String,KeyCode> keyList = new Dictionary<String,KeyCode>();

        public MissionLoader(string textFile)
        { 
            missionFile = System.IO.File.ReadAllLines((Resources.PathToResource(textFile)));
        }

        public void DoAction(GameController gc)
        {
            List<DestroyableObject> charList = new List<DestroyableObject>();
            List<Ability> abilitylist = new List<Ability>();
            AbilitySet tempAbilitySet = new AbilitySet();
            List<GNParticles> gnDrive;
            Stage newStage = new Stage(true);
            Player p;
            Character c;
            KeyCode k;

            

            string name, gundamPath;
            string[] splitString;
            Sprite gundamSprite;
            bool isPlayer;
            int fpc, cells,line = 4,
                shootOffsetX, shootOffsetY,
                offsetX, offsetY,
                driveOffsetY;


            for (int i = 0; i < int.Parse(missionFile[1]); i++)
            {
                isPlayer = missionFile[line++] == "Player";
                name = missionFile[line++];
                gundamPath = missionFile[line++];

                splitString = missionFile[line++].Split(',');
                fpc = int.Parse(splitString[0]);
                cells = int.Parse(splitString[1]);

                gundamSprite = Sprites.CreateSprite(Resources.LoadBitmap(Resources.PathToResource(gundamPath)), fpc, cells, GlobalConst.GundamWidth, GlobalConst.GundamWidth);
                GlobalConst.Utils.ActivateAnimationRow(gundamSprite, GlobalConst.DefaultRow, GlobalConst.DefaultColEnd, GlobalConst.DefaultFPC);

                splitString = missionFile[line++].Split(',');
                shootOffsetX = gundamSprite.Width / 2 - int.Parse(splitString[0]);
                shootOffsetY = gundamSprite.Height / 2 - int.Parse(splitString[1]);

                p = new Player(name, gundamSprite, shootOffsetX, shootOffsetY, 50000, new ZakuAI());
                p.State = GlobalConst.SpriteState.normalWest;

                if (isPlayer)
                {
                    driveOffsetY = int.Parse(missionFile[line++]);
                    gnDrive = new List<GNParticles>();
                    for (int j = 0; j < 100; j++)
                    {
                        gnDrive.Add(new GNParticles((int)(gundamSprite.X + gundamSprite.Width / 2) + p.DriveOffSetX(), (int)(gundamSprite.Y + gundamSprite.Height / 2) - driveOffsetY, p));
                        p.GnDriveParticles = gnDrive;
                    }
                    p.MaxPower = int.Parse(missionFile[line++]);
                    p.StateRow = int.Parse(missionFile[line++]);
                }

                charList.Add(p);

                /*          k = missionFile[line++].;
               
                

                

                          Ability newAbility = new MovementAbility(c, false, false, KeyCode.vk_RIGHT, "Right", 1, 1, 1, 7);
                          newAbility.AbilityAction = new Movement(c, Movement.Direction.Right);
                          abilitylist.Add(newAbility);

                          newAbility = new MovementAbility(c, false, false, KeyCode.vk_LEFT, "Left", 1, 2, 1, 7);
                          newAbility.AbilityAction = new Movement(c, Movement.Direction.Left);
                          abilitylist.Add(newAbility);

                          newAbility = new MovementAbility(c, false, false, KeyCode.vk_UP, "Up", 1, row, colEnd, count);
                          newAbility.AbilityAction = new Movement(c, Movement.Direction.Up);
                          abilitylist.Add(newAbility);

                          newAbility = new MovementAbility(c, false, false, KeyCode.vk_DOWN, "Down", 1, row, colEnd, count);
                          newAbility.AbilityAction = new Movement(c, Movement.Direction.Down);
                          abilitylist.Add(newAbility);

                          newAbility = new InstantAbility(c, false, false, KeyCode.vk_EQUALS, "Unleash Particles", 0, row, colEnd, count);
                          newAbility.Icon = Resources.LoadBitmap("hotkeys.png");
                          newAbility.AbilityAction = new AdjustParticles(c, AdjustParticles.Adjust.Add);
                          abilitylist.Add(newAbility);

                          newAbility = new InstantAbility(c, false, false, KeyCode.vk_MINUS, "Supress Particles", 0, row, colEnd, count);
                          newAbility.Icon = Resources.LoadBitmap("hotkeys.png");
                          newAbility.AbilityAction = new AdjustParticles(c, AdjustParticles.Adjust.Subtract);
                          abilitylist.Add(newAbility);

                          newAbility = new Melee(c, 5, 10, false, true, KeyCode.vk_z, "Punch Right", 2, 3, 4, 7);
                          newAbility.Icon = Resources.LoadBitmap("hotkeys.png");
                          newAbility.AbilityAction = new DealDamage(c, newAbility);
                          abilitylist.Add(newAbility);

                          newAbility = new Melee(c, 5, 10, false, true, KeyCode.vk_x, "Punch Left", 2, 4, 4, 7);
                          newAbility.Icon = Resources.LoadBitmap("hotkeys.png");
                          newAbility.AbilityAction = new DealDamage(c, newAbility);
                          abilitylist.Add(newAbility);

                          newAbility = new Shoot(c, false, true, KeyCode.vk_c, "Shoot", 2, 4, 4, 7);
                          newAbility.Icon = Resources.LoadBitmap("hotkeys.png");
                          newAbility.AbilityAction = new BasicShot(c, newAbility);
                          abilitylist.Add(newAbility);

                          newAbility = new InstantAbility(c, false, false, KeyCode.vk_1, "Summon", 0, row, colEnd, count);
                          newAbility.Icon = Resources.LoadBitmap("hotkeys.png");
                          newAbility.AbilityAction = new Summon();
                          abilitylist.Add(newAbility);

                          newAbility = new Shoot(c, false, true, KeyCode.vk_a, "Touhou Ability1", 3, 4, 4, 7);
                          newAbility.Icon = Resources.LoadBitmap("hotkeys.png");
                          newAbility.AbilityAction = new Touhou1(c, newAbility, GlobalConst.ScreenWidth / 2, GlobalConst.ScreenHeight / 2, 5);
                          abilitylist.Add(newAbility);

                          newAbility = new Shoot(c, false, true, KeyCode.vk_s, "Touhou Ability2", 3, 4, 4, 7);
                          newAbility.Icon = Resources.LoadBitmap("hotkeys.png");
                          newAbility.AbilityAction = new Touhou2(gc, c, newAbility, GlobalConst.ScreenWidth / 2, GlobalConst.ScreenHeight / 2, 5);
                          abilitylist.Add(newAbility);

                          newAbility = new TimedDurationAbility(c, false, true, KeyCode.vk_2, "TransAm", 0, row, colEnd, count);
                          newAbility.Icon = Resources.LoadBitmap("hotkeys.png");
                          newAbility.AbilityAction = new TransAm("TransAm", c, newAbility, 15000);
                          abilitylist.Add(newAbility);

                          newAbility = new TimedDurationAbility(c, false, true, KeyCode.vk_d, "Gravity", 0, row, colEnd, count);
                          newAbility.Icon = Resources.LoadBitmap("hotkeys.png");
                          newAbility.AbilityAction = new Gravity(newAbility);
                          abilitylist.Add(newAbility);

                          newAbility = new TimedDurationAbility(c, false, true, KeyCode.vk_q, "Explode", 0, row, colEnd, count);
                          newAbility.Icon = Resources.LoadBitmap("hotkeys.png");
                          newAbility.AbilityAction = new Explode(c, newAbility);
                          abilitylist.Add(newAbility);

                          newAbility = new TimedDurationAbility(c, false, true, KeyCode.vk_w, "Void Zone", 0, row, colEnd, count);
                          newAbility.Icon = Resources.LoadBitmap("hotkeys.png");
                          newAbility.AbilityAction = new VoidZones();
                          abilitylist.Add(newAbility);

                          newAbility = new TimedDurationAbility(c, false, true, KeyCode.vk_e, "Balance", 0, row, colEnd, count);
                          newAbility.Icon = Resources.LoadBitmap("hotkeys.png");
                          newAbility.AbilityAction = new NetherSpite(c);
                          abilitylist.Add(newAbility);

                          tempAbilitySet.Name = "Test";
                          tempAbilitySet.SetOfAbilities = abilitylist;

                          c.CurrentSet = tempAbilitySet;
                          c.SetofAbiities.Add(c.CurrentSet);

                

                          charList.Add(c);

                          name = "Zaku_01";
                          gundamSprite = Sprites.CreateSprite(Resources.LoadBitmap(Resources.PathToResource("CharSprites\\zaku.png")), 5, 1, 320, 240);

                          row = 0; colEnd = 1; count = 7;
                          driveOffSetX = 10; driveOffSetY = 60;
                          shootOffsetX = gundamSprite.Width / 2 - 30;
                          shootOffsetY = gundamSprite.Height / 2 - 40;

                          abilitylist = new List<Ability>();
                          gnDrive = new List<GNParticles>();

                          GlobalConst.Utils.ActivateAnimationRow(gundamSprite, row, colEnd, count);

                          Character ch = new Character(name, gundamSprite, shootOffsetX, shootOffsetY, false, 500, new ZakuAI());
                          ch.StateRow = 1;
                          ch.State = GlobalConst.SpriteState.normalEast;

                          charList.Add(ch);

                          name = "Dynames";
                          gundamSprite = Sprites.CreateSprite(Resources.LoadBitmap(Resources.PathToResource("CharSprites\\dynames.png")), 5, 100, 320, 240);
                          gundamSprite.X = 500;
                          gundamSprite.Y = 500;

                          row = 0; colEnd = 1; count = 7;
                          driveOffSetX = 10; driveOffSetY = 60;
                          shootOffsetX = gundamSprite.Width / 2 - 30;
                          shootOffsetY = gundamSprite.Height / 2 - 40;

                          abilitylist = new List<Ability>();
                          gnDrive = new List<GNParticles>();
                          tempAbilitySet = new AbilitySet();

                          GlobalConst.Utils.ActivateAnimationRow(gundamSprite, row, colEnd, count);

                          ch = new Character(name, gundamSprite, shootOffsetX, shootOffsetY, false, 500000, new DynamesAI());

                          newAbility = new Melee(ch, 5, 10, false, true, KeyCode.vk_HOME, "Slash", 2, 7, 5, 7);
                          newAbility.Icon = Resources.LoadBitmap("hotkeys.png");
                          newAbility.AbilityAction = new DealDamage(c, newAbility);
                          abilitylist.Add(newAbility);

                          newAbility = new MovementAbility(ch, false, false, KeyCode.vk_k, "Right", 1, 8, 1, 7);
                          newAbility.AbilityAction = new Movement(ch, Movement.Direction.Right);
                          abilitylist.Add(newAbility);

                          newAbility = new MovementAbility(ch, false, false, KeyCode.vk_j, "Left", 1, 9, 1, 7);
                          newAbility.AbilityAction = new Movement(c, Movement.Direction.Left);
                          abilitylist.Add(newAbility);

                          newAbility = new MovementAbility(ch, false, false, KeyCode.vk_i, "Up", 1, row, colEnd, count);
                          newAbility.AbilityAction = new Movement(ch, Movement.Direction.Up);
                          abilitylist.Add(newAbility);

                          newAbility = new MovementAbility(ch, false, false, KeyCode.vk_m, "Down", 1, row, colEnd, count);
                          newAbility.AbilityAction = new Movement(ch, Movement.Direction.Down);
                          abilitylist.Add(newAbility);

                          newAbility = new TimedDurationAbility(ch, false, true, KeyCode.vk_d, "Gravity", 0, row, colEnd, count);
                          newAbility.Icon = Resources.LoadBitmap("hotkeys.png");
                          newAbility.AbilityAction = new Gravity(newAbility);
                          abilitylist.Add(newAbility);

                          newAbility = new TimedDurationAbility(ch, true, true, KeyCode.vk_e, "Balance", 0, row, colEnd, count);
                          newAbility.Icon = Resources.LoadBitmap("hotkeys.png");
                          newAbility.AbilityAction = new NetherSpite(ch);
                          abilitylist.Add(newAbility);

                          tempAbilitySet.SetOfAbilities = abilitylist;
                          ch.CurrentSet = tempAbilitySet;
                          ch.StateRow = 10;
                          ch.State = GlobalConst.SpriteState.normalEast;

                
                      } */
            }
            newStage.CharList = charList;
            gc.GameStages.Pop();
            gc.GameStages.Pop();
            Music.Stop();
            gc.GameStages.Push(newStage);            
        }
    }
}
